package com.kzd.gtgame.jetpack.manager

import com.blankj.utilcode.util.AppUtils
import com.kzd.gtgame.app.*
import com.kzd.gtgame.base.BaseManager
import com.kzd.gtgame.entity.*
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.HttpResponseDataE
import com.module.lemlin.http.HttpResponseDataT
import com.module.lemlin.owner.OwnerApplication
import kotlinx.coroutines.Dispatchers
import rxhttp.*
import rxhttp.wrapper.entity.ProgressT
import top.zibin.luban.Luban
import java.io.IOException

class UserRepository : BaseManager() {

    /**
     * 检测更新
     * */
    suspend fun userAppUpDate(delay: Long = 0): HttpResponseDataT<AppUpdate> =
        RxHttp.get(APP_IS_UPDATE)
            .add("versionCode", AppUtils.getAppVersionCode().toLong())
            .toClass<HttpResponseDataT<AppUpdate>>()
            .delay(delay)
            .await()

    /**
     * 侧边浮标
     * */
    suspend fun userAppFab(): HttpResponseDataE<HomeFab> =
        RxHttp.postForm(NEW_APPS_GET_INDEX_ACTIVITY)
            .await()

    /**
     * 获取用户消息和代金卷状态 NEW_APPS_GET_TIPS
     * */
    suspend fun userAppTips(delay: Long = 0): HttpResponseDataT<AppTips> =
        RxHttp.postForm(NEW_APPS_GET_TIPS)
            .toClass<HttpResponseDataT<AppTips>>()
            .delay(delay)
            .await()

    /**
     * 获取用户信息
     * */
    suspend fun userInfo(): HttpResponseDataT<UserInfo> =
        RxHttp.postForm(USER_GET_USER_INFO)
            .await()

    /**
     * 获取我的页面信息
     * */
    suspend fun userMine(): HttpResponseDataT<UserMine> =
        RxHttp.postForm(USER_MY_INFO)
            .await()

    /**
     * 修改用户信息
     * */
    suspend fun userChange(
        sign: String, time: String,
        face: String? = null, address: String? = null,
        pet_name: String? = null, gender: String? = null,
        qq: String? = null, name: String? = null, idcard: String? = null,
    ): HttpResponseDataT<UserInfo> {
        val rxHttp = RxHttp.postForm(USER_CHANGE_USER_INFO)
            .add("sign", sign)
            .add("t", time)
        if (!face.isNullOrEmpty())
            rxHttp.add("face", face)
        if (!address.isNullOrEmpty())
            rxHttp.add("address", address)
        if (!pet_name.isNullOrEmpty())
            rxHttp.add("pet_name", pet_name)
        if (!gender.isNullOrEmpty())
            rxHttp.add("gender", gender)
        if (!qq.isNullOrEmpty())
            rxHttp.add("qq", qq)
        if (!name.isNullOrEmpty())
            rxHttp.add("name", name)
        if (!idcard.isNullOrEmpty())
            rxHttp.add("idcard", idcard)
        return rxHttp.await()
    }

    /**
     * 游戏收藏
     * */
    suspend fun userCollectAdd(
        sign: String, time: String, gid: String, type: String,
    ): HttpResponseData = RxHttp.postForm(APP_SHOUCANG_ADD)
        .add("sign", sign)
        .add("t", time)
        .add("id", gid)
        .add("type", type)
        .await()

    /**
     * 取消游戏收藏
     * */
    suspend fun userCollectDel(
        sign: String, time: String, gid: String, type: String,
    ): HttpResponseData = RxHttp.postForm(APP_SHOUCANG_DEL)
        .add("sign", sign)
        .add("t", time)
        .add("id", gid)
        .add("type", type)
        .await()

    /**
     * 代金券领取
     * */
    suspend fun userCouponGet(cid: String): HttpResponseDataT<WelfareCoupon> =
        RxHttp.postForm(USER_ADD_COUPON)
            .add("id", cid)
            .await()

    /**
     * 礼包领取
     * */
    suspend fun userGiftGet(
        sign: String, time: String, lid: String,
    ): WelfareGiftGetResponse = RxHttp.postForm(APP_GET_LB)
        .add("sign", sign)
        .add("t", time)
        .add("id", lid)
        .await()

    /**
     *
     * */
    suspend fun userUpload(
        fileName: String? = null, images: MutableList<String>,
    ): HttpResponseDataE<String> {
        val files = try {
            Luban.with(OwnerApplication.application).load(images).get()
        } catch (e: IOException) {
            null
        }?.map { it.path }?.toMutableList()

        return RxHttp.postForm(NEW_APPS_UPLOAD_IMGS)
            .add("fileName", fileName)
            .addFiles("image[]", files ?: images)
            .await()
    }

    suspend fun userDownload(
        urlPath: String, destPath: String,
        progress: (suspend (ProgressT<String>) -> Unit)? = null,
    ): HttpResponseDataT<String> {
        val path = RxHttp.get(urlPath).toDownload(destPath, Dispatchers.Main, progress)
            .await()
        return HttpResponseDataT(path).apply {
            this.code = 1
            this.message = "下载成功"
        }
    }

    /**
     * 邀请好友
     * */
    suspend fun userInvite(
        sign: String, time: String,
    ): HttpResponseDataT<UserInvite> = RxHttp.postForm(APP_APP_FRIEND)
        .add("sign", sign)
        .add("t", time)
        .await()

    /**
     * 协议
     * */
    suspend fun userAgreement(type: String): HttpResponseDataT<UserAgree> =
        RxHttp.postForm(NEW_APPS_GET_AGREEMENT)
            .add("type", type)
            .await()

    /**
     * 发表评论
     * */
    suspend fun userDiscussIssue(
        id: String,
        content: String,
        reply_uid: String? = null,
        pid: String? = null,
        image: String? = null,
    ): HttpResponseData {
        val http = RxHttp.postForm(PERSONAL_ADD_PINGLUN)
            .add("id", id)
            .add("content", content)
        if (!reply_uid.isNullOrEmpty()) {
            http.add("reply_uid", reply_uid)
        }
        if (!pid.isNullOrEmpty()) {
            http.add("pid", pid)
        }
        if (!image.isNullOrEmpty()) {
            http.add("image", image)
        }
        return http.await()
    }

    suspend fun userService(): HttpResponseDataT<UserService> =
        RxHttp.postForm(NEW_APPS_GET_KEFU_INFO)
            .await()

    /**
     * 收货地址列表
     * */
    suspend fun userAddress(
        sign: String, time: String,
    ): HttpResponseDataE<UserAddress> = RxHttp.postForm(APP_MY_ADDRESS)
        .add("sign", sign)
        .add("t", time)
        .await()

    /**
     * 保存收货地址
     * */
    suspend fun userAddressKeep(
        id: String? = null, consignee: String, mobile: String, uaddress: String, moren: String,
    ): HttpResponseDataE<UserAddress> {
        val http = RxHttp.postForm(USER_CHANGE_ADDRESS)
            .add("consignee", consignee)
            .add("mobile", mobile)
            .add("uaddress", uaddress)
            .add("moren", moren)
        if (!id.isNullOrEmpty()) {
            http.add("id", id)
        }
        return http.await()
    }

    /**
     * 收货地址列表
     * */
    suspend fun userInstallFinish(device_id: String?): HttpResponseData =
        RxHttp.postForm(APP_INSTALL_FINISH)
            .add("device_id", device_id ?: "")
            .await()
}